﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using AssetManagementSystem.Model;
using AssetManagementSystem.Utils.Events;
using AssetManagementSystem.View.IView;
using AssetManagementSystem.Utils;

namespace AssetManagementSystem.Presenter {

    class StudentPresenter {

        StudentManager manager;
        LoanManager loanManager;
        IStudentView view;

        public StudentPresenter(IStudentView view) {
            manager = new StudentManager();
            loanManager = new LoanManager();
            this.view = view;
            //Handle Student View Events
            this.view.Students_LoadAll += new EventHandler(PresentAllStudents);
            this.view.Students_Find += new EventHandler<SearchEventArgs>(PresentFoundStudents);
            this.view.Student_Create += new EventHandler<StudentEventArgs>(CreateStudent);
            this.view.Student_Update += new EventHandler<StudentEventArgs>(UpdateStudent);
            this.view.Student_Delete += new EventHandler<StudentEventArgs>(DeleteStudent);

        }

        /// <summary>
        /// Provides a list of all students to the view for display
        /// </summary>
        /// <param name="sender">event sender</param>
        /// <param name="e">event args</param>
        private void PresentAllStudents(object sender, EventArgs e) {
            view.SetStudentList(manager.GetAllStudent());
        }

        /// <summary>
        /// Provides a list of students found based on search data provided in event args
        /// </summary>
        /// <param name="sender">event sender</param>
        /// <param name="e">event args, must contain search input</param>
        private void PresentFoundStudents(object sender, SearchEventArgs e) {
            view.SetStudentList(manager.FindStudents(e.SearchText));
        }

        /// <summary>
        /// Tries to create a new student and refresh the view
        /// </summary>
        /// <param name="sender">event sender</param>
        /// <param name="e">args</param>
        private void CreateStudent(object sender, StudentEventArgs e) {
            try {
                //store the new student
                manager.CreateStudent(e.Student);
                //refresh the students on the view
                PresentAllStudents(this, null);
                view.ShowMessage("Student details successfully saved.");
            } catch (Exception createFailed) {
                //if something goes wrong, send a message to the view
                view.ShowMessage(createFailed.Message);
            }
        }

        /// <summary>
        /// Tries to update an existing student and refresh the view
        /// </summary>
        /// <param name="sender">event sender</param>
        /// <param name="e">args</param>
        private void UpdateStudent(object sender, StudentEventArgs e) {
            try {
                //update the student
                manager.UpdateStudent(e.Student);
                //refresh the students on the view
                PresentAllStudents(this, null);
                view.ShowMessage("Student details successfully updated.");
            } catch (Exception updateFailed) {
                //if something goes wrong, send a message to the view
                view.ShowMessage(updateFailed.Message);
            }
        }

        /// <summary>
        /// Tries to delete an existing student and refresh the view
        /// </summary>
        /// <param name="sender">event sender</param>
        /// <param name="e">args</param>
        private void DeleteStudent(object sender, StudentEventArgs e) {
            try {
                //delete the student
                manager.DeleteStudent(e.Student.StudentId);
                //refresh the students on the view
                PresentAllStudents(this, null);
                view.ShowMessage("Student details successfully deleted.");
            } catch (Exception deleteFailed) {
                //if something goes wrong, send a message to the view
                view.ShowMessage(deleteFailed.Message);
            }
        }
        
    }
}
